﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.Data;
using Sgr.EntityFrameworkCore.EntityConfigurations;
using Sgr.UPMS.Domain.Roles;
using Sgr.UPMS.Domain.Tokens;

namespace Sgr.UPMS.Infrastructure.EntityConfigurations
{
    /// <summary>
    /// 临时令牌实体配置
    /// </summary>
    internal class TemporaryTokenEntityTypeConfiguration : EntityTypeConfigurationBase<TemporaryToken, long>
    {
        public TemporaryTokenEntityTypeConfiguration(DatabaseType? databaseType = null) : base(databaseType)
        { }

        public override void Configure(EntityTypeBuilder<TemporaryToken> builder)
        {
            builder.ToTable("upms_temporary_tokens");

            base.Configure(builder);

            builder.Property(t => t.Code)
                .IsRequired()
                .HasMaxLength(100)
                .HasComment("令牌编码");

            builder.Property(t => t.Content)
                .HasMaxLength(200)
                .HasComment("令牌内容");

            builder.Property(t => t.TokenType)
                .IsRequired()
                .HasComment("令牌类型");

            builder.Property(t => t.DeviceFingerprint)
                .HasMaxLength(200)
                .HasComment("关联的设备指纹");

            builder.Property(t => t.UserId)
                .HasMaxLength(200)
                .HasComment("关联的用户Id");

            builder.Property(t => t.CreationTime)
                .IsRequired()
                .HasComment("创建时间");

            builder.Property(t => t.ExpirationTime)
                .IsRequired()
                .HasComment("过期时间");

            builder.Property(t => t.IsUsed)
                .IsRequired()
                .HasComment("是否已使用");

            // 创建唯一索引
            builder.HasIndex(t => t.Code)
                .IsUnique();
        }
    }
}